package com.github.common.extend.demo.config.json.jaskson.sensitive;

import org.apache.commons.lang3.StringUtils;

/**
 * @author X1993
 * @date 2022/08/12
 * @description
 */
public class SensitiveInfoUtils {

    public static String name(final String fullName) {
        if (fullName == null) {
            return null;
        }
        return chineseName(fullName);
    }


    public static String nickName(String nickname) {
        if (nickname == null) {
            return null;
        }
        int length = nickname.length();
        return desensitization(nickname, 1, length - 1);
    }

    private static String chineseName(final String name) {
        if(StringUtils.isBlank(name)){
            return StringUtils.EMPTY;
        }
        int length = name.length();
        String overlay = StringUtils.overlay(name, "*", 1, length > 2 ? length - 1:length);
        return overlay;
    }

    private static String englishName(final String fullName) {
        String[] names = fullName.split("[\\s]+");
        String firstName = "";
        String middleName = "";
        String lastName = "";
        if (names.length == 1) {
            firstName = names[0];
        } else if (names.length == 2) {
            firstName = names[0];
            lastName = names[1];
        } else {
            firstName = names[0];
            middleName = names[1];
            lastName = names[2];
        }
        if (firstName.length() > 1) {
            firstName = firstName.substring(0, 1) + ".";
        }
        if (middleName.length() > 1 && !(lastName.endsWith("Sr.") || lastName.endsWith("Jr.") || lastName.endsWith("Jun"))) {
            middleName = firstName.substring(0, 1) + ".";
        }
        StringBuilder sb = new StringBuilder(fullName.length());
        if (firstName.length() > 0) {
            sb.append(firstName);
        }
        if (middleName.length() > 0) {
            sb.append(" ");
            sb.append(middleName);
        }
        if (lastName.length() > 0) {
            sb.append(" ");
            sb.append(lastName);
        }
        return sb.toString();
    }

    /**
     * 身份证号脱敏 格式：33************9834
     * @param id
     * @return
     */
    public static String idCardNum(final String id) {
        if(StringUtils.isBlank(id)){
            return StringUtils.EMPTY;
        }
        String overlay = StringUtils.overlay(id, "************", 2, id.length()-4);
        return overlay;
    }


    public static String fixedPhone(final String num) {
        if (num == null) {
            return null;
        }
        int length = num.length();

        return desensitization(num, 0, length - 4);
    }

    /**
     * 手机号脱敏，前3后4显示，格式：133****7690
     * @param num
     * @return
     */
    public static String mobilePhone(final String num) {
        if(StringUtils.isBlank(num)){
            return StringUtils.EMPTY;
        }
        String overlay = StringUtils.overlay(num, "****", 3, num.length() - 4);
        return overlay;
    }


    public static String address(final String address, final int sensitiveSize) {
        if (address == null) {
            return null;
        }
        int length = address.length();

        return desensitization(address, sensitiveSize, length);
    }


    public static String email(final String email) {
        if (email == null) {
            return null;
        }
        int index = email.indexOf('@');
        if (index < 0) {
            return email;
        }


        return desensitization(email, 3, index);
    }


    public static String bankCard(final String cardNum) {
        if (cardNum == null) {
            return null;
        }
        int length = cardNum.length();
        if (length >= 16) {
            return desensitization(cardNum, 6, length - 4);
        } else {
            return defaultDesensitization(cardNum);
        }
    }


    //缺省规则
    //个人信息展示应遵循最小化原则脱敏展示进行保护。缺省信息隐藏规则:显
    //示前 1/3 和 后 1/3，其他用*号代替。内容长度不能被 3 整除时，显示前
    //length/3.0 取整+1 和后 length/3.0 取整。

    private static int getDefaultBeginIndex(int length) {
        if (length % 3 == 0) {
            return length / 3;
        }
        return length / 3 + 1;
    }

    public final static int getDefaultEndIndex(int length) {
        return length - length / 3;
    }

    public final static String defaultDesensitization(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        str = str.trim();
        int begin = getDefaultBeginIndex(str.length());
        int end = getDefaultEndIndex(str.length());
        return desensitization(str, begin, end);
    }

    public final static String desensitization(String str, int begin, int end) {
        StringBuilder builder = new StringBuilder(str);
        for (int i = begin; i < end && i < builder.length(); i++) {
            if (i >= 0)
                builder.setCharAt(i, '*');
        }
        return builder.toString();
    }

}